﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

using ECCIT;
using ECCIT.Utilities;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.BLL;

public partial class GateService_SearchGate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        string output = string.Empty;
        try
        {
            ResultInfo valRes = ValidateArguments();

            if (valRes.Result)
            {
                string sLangID = Request.Params["lang"];
                string encKey = Request.Params["key"];
                string type = Request.Params["type"];
                int pageSize = int.Parse(Request.Params["pageSize"]);
                int currPageIndex = int.Parse(Request.Params["pageIndex"]);

                PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currPageIndex, pageSize, null);
                int langID = (string.IsNullOrEmpty(sLangID)) ? 2052 : int.Parse(sLangID);
                //string key = StringUtil.FromBase64String(encKey, (langID == 2052) ? Encoding.GetEncoding("GB2312") : Encoding.UTF8);
                string key = HttpUtility.UrlDecode(encKey);

                if (type == "0")
                {
                    output = JsonUtil.Serialize(Search(langID, key, pagingParams));
                }
                else if (type == "1")
                {
                    EventsImageBLL imageBll = new EventsImageBLL(langID);
                    output = JsonUtil.Serialize(imageBll.GetEventsImageModelsByKeywords(key, pageSize, currPageIndex));
                }
                else if (type == "2")
                {
                    EventsVideoBLL videoBll = new EventsVideoBLL(langID);
                    output = JsonUtil.Serialize(videoBll.GetEventsVideoModelsByKeywords(key, pageSize, currPageIndex));
                }
            }
        }
        catch (Exception ex)
        {
            output = "<files value=\"false\"></files>";
        }

        Response.Write(output);
    }

    private ResultInfo ValidateArguments()
    {
        ResultInfo result = new ResultInfo(false);
        string sLangID = Request.Params["lang"];
        if (string.IsNullOrEmpty(sLangID)) return result;

        int langID;
        bool b = int.TryParse(sLangID, out langID);
        if (!b) return result;
        if (langID != 2052 && langID != 1033) return result;

        result.Result = true;
        return result;
    }

    private PagingList Search(int langID, string key, PagingParameter pagingParams)
    {
        SearchBll searchBll = new SearchBll();
        searchBll.FilePath = Path.Combine(Server.MapPath("~"), (langID == (int)LanguageType.cn) ? SiteConfig.Search.CnFilePath : SiteConfig.Search.EnFilePath);
        searchBll.IndexPath = Path.Combine(Server.MapPath("~"), (langID == (int)LanguageType.cn) ? SiteConfig.Search.CnIndexPath : SiteConfig.Search.EnIndexPath);
        searchBll.IsCreate = true;

        if (!string.IsNullOrEmpty(key))
            searchBll.SearchKey = key.Trim();

        PagingList list = searchBll.QueryByParagraph(pagingParams);
        return list;
    }
}